#include "PCResource_theme_light.h"

LV_FONT_DECLARE(lv_font_tiger_30)
LV_FONT_DECLARE(lv_font_tiger_40)
LV_FONT_DECLARE(lv_font_tiger_60)
LV_FONT_DECLARE(lv_font_tiger_80)

LV_IMG_DECLARE(cpu_light)
LV_IMG_DECLARE(gpu_light)
LV_IMG_DECLARE(ram_light)
LV_IMG_DECLARE(disk_light)
LV_IMG_DECLARE(up_light)
LV_IMG_DECLARE(down_light)

typedef struct _module module_t;

typedef void (*module_panel_update_t)(void *, PCResInfo *);
typedef void (*module_panel_create_t)(module_t *);

typedef struct _module
{
  lv_obj_t *cont;       // 组件容器
  lv_obj_t *img;        // 组件图标
  lv_obj_t *title;      // 组件名称
  lv_obj_t *data_label; // 数据标签
  lv_obj_t *panel_cont; // 详细面板容器
} module_t;

static void pc_resource_gui_light_theme_init(lv_obj_t *parent, gui_opt_t *opts);
static void pc_resource_gui_light_theme_update(PCResInfo *info);
static void pc_resource_gui_light_theme_release(void);
static void pc_resource_gui_light_time_label_click_event_cb(lv_event_t *e);
static void pc_resource_gui_light_time_label_long_press_event_cb(lv_event_t *e);

gui_theme_operations light_theme =
    {
        .init = pc_resource_gui_light_theme_init,
        .update = pc_resource_gui_light_theme_update,
        .release = pc_resource_gui_light_theme_release,
};

// 回调函数集合
static gui_opt_t *gui_opts;

// 背景图片
static lv_obj_t *back_img;

// 6个组件
static module_t module[6];

// 时间标签
static lv_obj_t *time_label;

// 组件名称
static const char *module_tile[] =
    {
        "CPU", "GPU", "RAM", "SSD", "UP", "DOWN"};

// 组件图标
static const lv_img_dsc_t *module_img[] =
    {
        &cpu_light, &gpu_light, &ram_light, &disk_light, &up_light, &down_light};

// 组件数据标签
static const char *module_data[] =
    {
        "0°", "0°", "0%", "0°", "0", "0"};

/**
 *@brief light主题初始化函数
 */
static void pc_resource_gui_light_theme_init(lv_obj_t *parent, gui_opt_t *opts)
{
  gui_opts = opts;

  // 创建背景图片;
  back_img = lv_img_create(parent);
  lv_obj_set_size(back_img, 1024, 600);
  lv_img_set_src(back_img, "S:./res/pc_resource/background/lightbg.bin");
  lv_obj_center(back_img);

  // 创建组件
  for (int i = 0; i < sizeof(module) / sizeof(module_t); i++)
  {
    // 创建组件容器
    lv_obj_t *cont = lv_obj_create(back_img);
    lv_obj_set_size(cont, 320, 175);
    lv_obj_clear_flag(cont, LV_OBJ_FLAG_SCROLLABLE);                           // 禁止对象滚动
    lv_obj_set_style_bg_opa(cont, LV_OPA_40, LV_STATE_DEFAULT);                // 背景透明度
    lv_obj_set_style_border_opa(cont, LV_OPA_TRANSP, LV_STATE_DEFAULT);        // 边框透明
    lv_obj_set_style_bg_color(cont, lv_color_hex(0x101010), LV_STATE_DEFAULT); // 背景黑色
    module[i].cont = cont;

    // 创建组件icon
    lv_obj_t *img = lv_img_create(cont);
    lv_obj_set_size(img, 100, 100);
    lv_img_set_src(img, module_img[i]);
    lv_obj_align(img, LV_ALIGN_LEFT_MID, -5, 0);
    lv_obj_set_style_img_opa(img, LV_OPA_80, LV_STATE_DEFAULT);
    module[i].img = img;

    // 创建组件标题
    lv_obj_t *title_label = lv_label_create(cont);
    lv_obj_set_style_text_font(title_label, &lv_font_tiger_40, LV_STATE_DEFAULT);       // 设置字体
    lv_obj_set_style_text_opa(title_label, LV_OPA_COVER, LV_STATE_DEFAULT);                // 字体透明度
    lv_obj_set_style_text_color(title_label, lv_color_hex(0xb2e8ff), LV_STATE_DEFAULT); // 字体颜色
    lv_label_set_text(title_label, module_tile[i]);
    lv_obj_align(title_label, LV_ALIGN_TOP_MID, 0, -15);
    module[i].title = title_label;

    // 创建组件数据标签
    lv_obj_t *data_label = lv_label_create(cont);
    lv_obj_set_style_text_font(data_label, &lv_font_tiger_80, LV_STATE_DEFAULT);       // 设置字体
    lv_obj_set_style_text_opa(data_label, LV_OPA_COVER, LV_STATE_DEFAULT);                // 字体透明度
    lv_obj_set_style_text_color(data_label, lv_color_hex(0xeaeaea), LV_STATE_DEFAULT); // 字体颜色
    lv_label_set_text(data_label, module_data[i]);
    lv_obj_align(data_label, LV_ALIGN_RIGHT_MID, 0, 0);
    module[i].data_label = data_label;

    if (i == 4 || i == 5) // 网络速率单位标签
    {
      lv_obj_t *unit_label = lv_label_create(cont);
      lv_obj_set_style_text_font(unit_label, &lv_font_tiger_30, LV_STATE_DEFAULT);       // 设置字体
      lv_obj_set_style_text_opa(unit_label, LV_OPA_90, LV_STATE_DEFAULT);                // 字体透明度
      lv_obj_set_style_text_color(unit_label, lv_color_hex(0xb2e8ff), LV_STATE_DEFAULT); // 字体颜色
      lv_label_set_text(unit_label, "KB/s");
      lv_obj_align_to(unit_label, data_label, LV_ALIGN_OUT_BOTTOM_RIGHT, 0, 0);
      lv_obj_set_user_data(data_label, unit_label); // 设置用户数据
    }
  }

  // 设置容器对齐
  lv_obj_set_pos(module[0].cont, 15, 15);
  lv_obj_align_to(module[1].cont, module[0].cont, LV_ALIGN_OUT_RIGHT_MID, 15, 0);
  lv_obj_align_to(module[2].cont, module[0].cont, LV_ALIGN_OUT_BOTTOM_MID, 0, 15);
  lv_obj_align_to(module[3].cont, module[2].cont, LV_ALIGN_OUT_RIGHT_MID, 15, 0);
  lv_obj_align_to(module[4].cont, module[2].cont, LV_ALIGN_OUT_BOTTOM_MID, 0, 15);
  lv_obj_align_to(module[5].cont, module[4].cont, LV_ALIGN_OUT_RIGHT_MID, 15, 0);

  // 创建时间标签
  time_label = lv_label_create(back_img);
  lv_obj_set_style_text_font(time_label, &lv_font_tiger_80, LV_STATE_DEFAULT);       // 设置字体
  lv_obj_set_style_text_opa(time_label, LV_OPA_90, LV_STATE_DEFAULT);                // 字体透明度
  lv_obj_set_style_text_color(time_label, lv_color_hex(0xffffff), LV_STATE_DEFAULT); // 字体颜色
  lv_label_set_text(time_label, "00:00");
  lv_obj_align(time_label, LV_ALIGN_TOP_RIGHT, -80, 60);

  // 为时间标签添加事件回调
  lv_obj_add_flag(time_label, LV_OBJ_FLAG_CLICKABLE);
  lv_obj_add_event_cb(time_label, pc_resource_gui_light_time_label_click_event_cb,
                      LV_EVENT_SHORT_CLICKED, NULL);
  lv_obj_add_event_cb(time_label, pc_resource_gui_light_time_label_long_press_event_cb,
                      LV_EVENT_LONG_PRESSED, NULL);
}

/**
 *@brief light主题数据更新函数
 */
static void pc_resource_gui_light_theme_update(PCResInfo *info)
{
  lv_label_set_text_fmt(module[0].data_label, "%d°", info->cpu_temp);
  lv_label_set_text_fmt(module[1].data_label, "%d°", info->gpu_temp);
  lv_label_set_text_fmt(module[2].data_label, "%d%%", info->ram_usage);
  lv_label_set_text_fmt(module[3].data_label, "%d°", info->disk_temp);

  int up = info->net_upload_speed;
  int down = info->net_download_speed;

  int up_keep_mininum = 1;
  int down_keep_mininum = 1;
  lv_obj_t *up_unit_label = lv_obj_get_user_data(module[4].data_label);
  lv_obj_t *down_unit_label = lv_obj_get_user_data(module[5].data_label);

  if (up > 100000) // 大于10MB/s
    up_keep_mininum = 10;

  if (down > 100000)
    down_keep_mininum = 10;

  if (up > 1000) // 大于100KB/s
  {
    up /= 100;
    // 单位变换
    lv_label_set_text(up_unit_label, "MB/s");
  }
  else
    lv_label_set_text(up_unit_label, "KB/s");

  if (down > 1000)
  {
    down /= 100;
    // 单位变换
    lv_label_set_text(down_unit_label, "MB/s");
  }
  else
    lv_label_set_text(down_unit_label, "KB/s");

  lv_label_set_text_fmt(module[4].data_label, "%d.%d", up / 100, up % 100 / up_keep_mininum);
  lv_label_set_text_fmt(module[5].data_label, "%d.%d", down / 100, down % 100 / down_keep_mininum);

  // 更新时间显示
  char time[6] = {"00:00"};

  time[0] += info->hour / 10;
  time[1] += info->hour % 10;
  time[3] += info->min / 10;
  time[4] += info->min % 10;

  lv_label_set_text(time_label, time);
}

/**
 *@brief light主题释放函数
 */
static void pc_resource_gui_light_theme_release(void)
{
  lv_obj_del_async(back_img);
}

/**
 *@brief 时间标签单击事件回调函数
 */
static void pc_resource_gui_light_time_label_click_event_cb(lv_event_t *e)
{
    if (gui_opts != NULL && gui_opts->set_next_theme != NULL)
      gui_opts->set_next_theme();
}

/**
 *@brief 时间标签长按事件回调函数
 */
static void pc_resource_gui_light_time_label_long_press_event_cb(lv_event_t *e)
{
    if (gui_opts != NULL && gui_opts->exit_cb != NULL)
      gui_opts->exit_cb(e);
}
